﻿<!DOCTYPE html>

<html>
<head>
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    
    <link rel="stylesheet" type="text/css" href="../miniui/themes/default/miniui.css" />
    <link rel="stylesheet" type="text/css" href="../miniui/themes/icons.css" />
    <script type="text/javascript" src="//fortunearterial.mysoft.com.cn:8001/jquery-2.0.3.js?ver=2.0.3"></script>
    <script src="../miniui/miniui.js" type="text/javascript"></script>
        
    <style type="text/css">
        html, body {
            padding: 0;
            margin: 0;
            border: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
        }
    </style>
</head>
<body>
    <div class="mini-fit" style="padding-top:10px;">
        
        <ul id="tree1" class="mini-tree" style="width:100%;height:100%;" 
            showTreeIcon="true" textField="Name" idField="id" parentField="pid" resultAsTree="false"  
            showCheckBox="true" checkRecursive="true"
            expandOnLoad="true" allowSelect="false" enableHotTrack="false" 
            >
        </ul>
    
    </div>                
    <div class="mini-toolbar" style="text-align:center;padding-top:8px;padding-bottom:8px;" 
        borderStyle="border-left:0;border-bottom:0;border-right:0;">
        <a class="mini-button" style="width:60px;" onclick="onOk()">确定</a>
        <span style="display:inline-block;width:25px;"></span>
        <a class="mini-button" style="width:60px;" onclick="onCancel()">取消</a>
    </div>

</body>
</html>
<script type="text/javascript">
    mini.parse();

    var tree = mini.get("tree1");

    $.ajax({
        url: '/Model/UserKnowledgeDetailsType/?userID=' + location.search.substring(1),
        type: 'GET'
    }).then(function (data) {
        var arrResult = [];
        $.each(data, function (i, json) {
            if (!json.ParentCode) {
                arrResult.push(json);
            };
        });
        var _findChildren = function (parentJson) {
            $.each(data, function (i, json) {
                if (json.ParentCode == (parentJson.ParentCode ? parentJson.ParentCode + '.' : '') + parentJson.Code) {
                    parentJson.children = parentJson.children || [];
                    parentJson.children.push(json);
                };
            });
            if (parentJson.children) {
                parentJson.children.forEach(function (result) {
                    _findChildren(result)
                });
            }
        };
        arrResult.forEach(function (result) {
            _findChildren(result)
        });

        console.info(arrResult)

        tree.loadData(arrResult);
    });

    function GetCheckedNodes() {
        var nodes = tree.getCheckedNodes();
        return nodes;
    }
    function GetData() {
        var nodes = tree.getCheckedNodes();
        var ids = [], texts = [];
        for (var i = 0, l = nodes.length; i < l; i++) {
            var node = nodes[i];
            if (tree.isLeaf(node)) {
                ids.push(node.id);
                texts.push(node.text);
            }
        }
        var data = {};
        data.id = ids.join(",");
        data.text = texts.join(",");
        return data;
    }
    function search() {
        var key = mini.get("key").getValue();
        //grid.load({ key: key });
    }
    function onKeyEnter(e) {
        search();
    }
    //////////////////////////////////
    function CloseWindow(action) {
        try{
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        } catch (ex) { }
        try {
            frameElement.api.doOK(GetData());
        } catch (ex) { }
    }
    function onOk() {
        var node = tree.getSelectedNode();
        if (node && tree.isLeaf(node) == false) {
            alert("不能选中父节点");
            return;
        }

        CloseWindow("ok");
    }
    function onCancel() {
        CloseWindow("cancel");
    }


</script>